#include "unix_socket.h"
#include "receive_frame.h"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#define FIFO_NAME "/tmp/client_fifo"
#define SERVER_NAME "../forwarding_server/foo.sock"
#define EXIT_FLAG '#'



void unix_socket_close(int sockfd)
{
	unsigned char tmp = EXIT_FLAG;
	write(sockfd, &tmp, 1);
	printf("client close.\n");
	sleep(2);
	close(sockfd);
	exit(0);
}


int conversion(unsigned char *temp, unsigned char *data, int len)
{
	int loop = 0;
	int loop_res = 0;
	int res = 0;

	for (loop_res = 0, loop = 0; loop < len; loop_res++, loop++)
	{
		if (temp[loop] == ' ')
		{
			loop_res--;
			continue;
		}
		if (temp[loop] >= 'A' && temp[loop] < 'Z')
		{
			temp[loop_res] = temp[loop] - 'A' + 10;
		}
		else if (temp[loop] >= 'a' && temp[loop] < 'z')
		{
			temp[loop_res] = temp[loop] - 'a' + 10;
		}
		else
		{
			temp[loop_res] = temp[loop] - '0';
		}
	}

	res = loop_res;

	for (loop = 0, loop_res = 0; loop_res < res / 2; loop++, loop_res++)
	{
		data[loop_res] = (temp[loop] << 4) | temp[loop + 1];
		loop++;
	}

	res = loop_res;

	return res;
}


void unix_pipe(int sockfd)
{
	int pipefd, res, len;
	unsigned char tmpbuf[128];
	unsigned char sendbuf[128];
	
	//creat fifo
	if (-1 == mkfifo(FIFO_NAME, 0777))
	{
		log_time = time(&log_time);
		memset(log_str, 0, 1024);
    sprintf(log_str, "%s  %-8s | line %-3d | %s : %s.\n", ctime(&log_time), __FILE__, __LINE__ - 4, "mkfifo", strerror(errno));
		printf("%s", log_str);
		if (-1 == fwrite(log_str, 1, strlen(log_str),fdlog))
		{
			perror("Log write error:");
		}
		fflush(fdlog);
		exit(-1);
	}

	//change the permissions of FIFO_NAME
	if (-1 == chmod(FIFO_NAME, 0666))
	{
		log_time = time(&log_time);
		memset(log_str, 0, 1024);
    sprintf(log_str, "%s  %-8s | line %-3d | %s : %s.\n", ctime(&log_time), __FILE__, __LINE__ - 4, "mkfifo", strerror(errno));
		printf("%s", log_str);
		if (-1 == fwrite(log_str, 1, strlen(log_str), fdlog))
		{
			perror("Log write error:");
		}
		fflush(fdlog);
		exit(-1);
	}


	printf("Creat fifo seccess.\n");


	pipefd = open(FIFO_NAME, O_RDWR);
	if (pipefd == -1)
	{
		log_time = time(&log_time);
		memset(log_str, 0, 1024);
    sprintf(log_str, "%s  %-8s | line %-3d | %s : %s.\n", ctime(&log_time), __FILE__, __LINE__ - 4, "mkfifo", strerror(errno));
		printf("%s", log_str);
		if (-1 == fwrite(log_str, 1, strlen(log_str), fdlog))
		{
			perror("Log write error:");
		}
		fflush(fdlog);
		exit(-1);
	}

	while (1)
	{
		res = read(pipefd, tmpbuf, sizeof(sendbuf));
		if (-1 == res)
		{
			log_time = time(&log_time);
			memset(log_str, 0, 1024);
			sprintf(log_str, "%s  %-8s | line %-3d | %s : %s.\n", ctime(&log_time), __FILE__, __LINE__ - 4, "mkfifo", strerror(errno));
			printf("%s", log_str);
			if (-1 == fwrite(log_str, 1, strlen(log_str), fdlog))
			{
				perror("Log write error:");
			}
			fflush(fdlog);
			continue;
		}
		else if (res == 0)
		{
			continue;
		}
		else
		{
			int i = 0;
			for (i = 0; i < res; i++)
			{
				printf("%c", tmpbuf[i]);
				if (tmpbuf[i] == EXIT_FLAG)
				{
					unix_socket_close(sockfd);
				}
			}
			
			len = conversion(tmpbuf, sendbuf, res);

			res = write(sockfd, sendbuf, len);
			printf("res = %d\n", res);
			if (res == -1)
			{
				printf("Error[%d] when writing.(%s)\n", errno, strerror(errno));
				continue;
			}
		}
		usleep(1000);

	}

}






int unix_socket_init (void)
{
	int sockfd;
	struct sockaddr_un address;
	
	sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sockfd == -1)
	{
		log_time = time(&log_time);
		memset(log_str, 0, 1024);
    sprintf(log_str, "%s  %-8s | line %-3d | %s : %s.\n", ctime(&log_time), __FILE__, __LINE__ - 4, "socket", strerror(errno));
		printf("%s", log_str);
		if (-1 == fwrite(log_str, 1, strlen(log_str), fdlog))
		{
			perror("Log write error:");
		}
		fflush(fdlog);
		exit(-1);
	}

	address.sun_family = AF_UNIX;
	strcpy(address.sun_path, SERVER_NAME);

	if (-1 == connect(sockfd, (struct sockaddr *)&address, sizeof(address)))
	{
		log_time = time(&log_time);
		memset(log_str, 0, 1024);
    sprintf(log_str, "%s  %-8s | line %-3d | %s : %s.\n", ctime(&log_time), __FILE__, __LINE__ - 4, "socket connect", strerror(errno));
		printf("%s", log_str);
		if (-1 == fwrite(log_str, 1, strlen(log_str), fdlog))
		{
			perror("Log write error:");
		}
		fflush(fdlog);
		exit(-1);
	}


	printf("Create socket success.\n");

	return sockfd;
}









